package com.game.yxy.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.game.yxy.common.Result;
import com.game.yxy.entity.CusFile;
import com.game.yxy.entity.CusQ;
import com.game.yxy.entity.ItemStatus;
import com.game.yxy.mapper.ItemStatusMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/itemstatus")
@Api(tags = "物品状态管理") // 描述整个控制器的作用
public class ItemStatusController {
    @Resource
    ItemStatusMapper itemStatusMapper;

    // 添加物品状态
    @PostMapping
    @ApiOperation(value = "添加物品状态", notes = "创建一个新的物品状态记录")
    public Result<?> save(@RequestBody ItemStatus itemStatus) {
        itemStatusMapper.insert(itemStatus);
        return Result.success();
    }

    // 更新物品状态
    @PutMapping
    @ApiOperation(value = "更新物品状态", notes = "根据ID更新物品状态记录")
    public Result<?> update(@RequestBody ItemStatus itemStatus) {
        itemStatusMapper.updateById(itemStatus);
        return Result.success();
    }

    // 删除物品状态
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除物品状态", notes = "根据ID删除物品状态记录")
    public Result<?> delete(@ApiParam(value = "物品状态ID", required = true) @PathVariable Long id) {
        itemStatusMapper.deleteById(id);
        return Result.success();
    }

    // 查询物品状态
    @GetMapping
    @ApiOperation(value = "查询物品状态", notes = "支持名字查询")
    public Result<?> findPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "50") Integer pageSize,
            @ApiParam(value = "名字", defaultValue = "") @RequestParam(defaultValue = "") String itname ){
        LambdaQueryWrapper<ItemStatus> wrapper = Wrappers.<ItemStatus>lambdaQuery();
        if (StrUtil.isNotBlank(itname)) {
            wrapper.like(ItemStatus::getItname, itname);
        }
        Page<ItemStatus> ItemStatusPage = itemStatusMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(ItemStatusPage);
    }

    // 根据ID查询物品状态
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询物品状态", notes = "根据物品状态ID查询物品状态记录")
    public Result<?> findById(@ApiParam(value = "物品状态ID", required = true) @PathVariable Long id){
        ItemStatus itemStatus = itemStatusMapper.selectById(id);
        if (itemStatus != null) {
            return Result.success(itemStatus);
        } else {
            return Result.failure("物品状态不存在");
        }
    }
}